﻿using Core.SimpleTemp.Application.UserApp;
using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Repositories;
using Core.Toolkit.Common;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Core.Toolkit.Core.Application;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System;

namespace Core.SimpleTemp.Application
{
    [AutoDi()]
    public class JG_STOPTimesService : BaseAppService<JG_STOPTimesDto, JG_STOPTimes, JG_STOPTimesRepository>
    {
        readonly ExcelHelp _excelHelp;
        private readonly SysUserAppService _userService;
        public JG_STOPTimesService(SysUserAppService userService,JG_STOPTimesRepository repository, ExcelHelp excelHelp) : base(repository)
        {
            _excelHelp = excelHelp;
            _userService = userService;
        }



        private LoadPageOffsetModelAuto<JG_STOPTimes, JG_STOPTimesDto>
            GetLoadPageOffsetMode(PagingModel<JG_STOPTimesDto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = this.GetLoadPageOffsetModelAuto(pagingQueryModel);
            var baseQuery = _repository.QueryBase().AsNoTracking().Include(u => u.SysDepartment).Include(d=>d.SysDuty).Include(n=>n.SysUser);
            loadPageOffsetModel.QueryWhere = this.CreateQRList().Add("CreateFactory","=")
                .Add("name", "=")
                .Add("department", "=");
            loadPageOffsetModel.BasqQuery = baseQuery;
            loadPageOffsetModel.Order = orderModel => orderModel.CreateTime;
            loadPageOffsetModel.Selector = u => new JG_STOPTimes() {
                Id =u.Id,
                department =u.department,SysDepartment=u.SysDepartment,
                SysDuty = u.SysDuty,
                SysUser =u.SysUser,
                IsWarn = u.IsWarn,
                WarnType = u.WarnType,
                WarnDays = u.WarnDays
            };
            return loadPageOffsetModel;
            //       var loadPageOffsetModel = this.GetLoadPageOffsetModelAuto(pagingQueryModel);
            //       var baseQuery = _repository.QueryBase().AsNoTracking().Include(de=>de.SysDepartment);
            //       loadPageOffsetModel.QueryWhere = this.CreateQRList()

            //.Add("department", "=") //部门
            //.Add("duty", "=") //职位
            //.Add("name", "=") //姓名
            //.Add("WarnTime", "=") //提醒频次
            //.Add("WarnBeginTime", "=") //提醒开始时间
            //.Add("WarnDays", "=") //提前提醒天数
            //.Add("CreateFactory", "=") //隶属工厂
            //.Add("CreateDept", "=") //添加部门
            //.Add("UpdateDept", "=");//更改部门


            //       loadPageOffsetModel.BasqQuery = baseQuery;
            //       loadPageOffsetModel.Order = orderModel => orderModel.CreateTime;
            //       loadPageOffsetModel.Selector = model => new JG_STOPTimes()
            //       {
            //           Id = model.Id,
            //           department = model.department,//部门
            //           duty = model.duty,//职位
            //           name = model.name,//姓名
            //           WarnTime = model.WarnTime,//提醒频次
            //           WarnBeginTime = model.WarnBeginTime,//提醒开始时间
            //           WarnDays = model.WarnDays,//提前提醒天数
            //           CreateFactory = model.CreateFactory,//隶属工厂
            //           CreateDept = model.CreateDept,//添加部门
            //           UpdateDept = model.UpdateDept,//更改部门
            //          SysDepartment = model.SysDepartment,
            //       };
            //       return loadPageOffsetModel;
        }


        public async Task<IPageModel<JG_STOPTimesDto>> GetList(PagingModel<JG_STOPTimesDto>
            pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            return await base.LoadPageOffsetAsync(loadPageOffsetModel);

        }


        public async Task<string>
            Export(PagingModel<JG_STOPTimesDto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            var DataList = await this.ExportDataAsync(loadPageOffsetModel);
            var excelData = DataList.Select(model => new
            {
                model.department,
                department_title = "部门",
                model.duty,
                duty_title = "职位",
                model.name,
                name_title = "姓名",
                model.WarnTime,
                WarnTime_title = "提醒频次",
                model.WarnBeginTime,
                WarnBeginTime_title = "提醒开始时间",
                model.WarnDays,
                WarnDays_title = "提前提醒天数",
                model.CreateFactory,
                CreateFactory_title = "隶属工厂",
                model.CreateDept,
                CreateDept_title = "添加部门",
                model.UpdateDept,
                UpdateDept_title = "更改部门",
            }).ToList();




            return _excelHelp.Create(excelData);
        }

        public async Task<SysUserDto> TestService(System.Guid id) 
        {
            var model = await _userService.GetAsync(id);
            return model;
        }

        public async Task<SysUserDto> GetNames(System.Guid departmentId,System.Guid dutyId)
        {
            var test = this._repository._dbContext.SysUser.ToList();
            return null;
        }

    }

}
